package com.sinocare.system.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sinocare.common.entity.Menu;
import com.sinocare.common.result.Result;
import com.sinocare.common.result.ResultCode;
import com.sinocare.system.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/menu")
@Api(tags = "菜单管理")
@CrossOrigin
public class MenuController {
    @Resource
    private MenuService menuService;

    /**
     *菜单管理查询
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "菜单管理查询",httpMethod ="GET")
    @RequestMapping(value = "menuAll",method = RequestMethod.GET)
    public Result findMenu(Integer page,Integer limit){
        Page page1 = PageHelper.startPage(page, limit);
        List<Menu> list = menuService.list();
       return Result.ok(ResultCode.SUCCESS,page1.getTotal(),list);
    }
    @ApiOperation(value = "修改菜单",httpMethod = "GET")
    @RequestMapping(value = "update",method = RequestMethod.GET)
    public Result update(Menu menu){
        int i = menuService.updateMenu(menu);
        if(i>0){
            return Result.ok(ResultCode.SUCCESS,i);
        }else{
            return Result.fail(ResultCode.FAIL,i);
        }

    }
    @ApiOperation(value = "增加菜单",httpMethod = "POST")
    @RequestMapping(value = "add",method = RequestMethod.POST)
    public Result add(Menu menu){
        System.out.println(menu);
        boolean i = menuService.save(menu);
        if (i){
            return Result.ok(ResultCode.SUCCESS,i);
        }else{
            return Result.fail(ResultCode.FAIL,i);
        }
    }
    @ApiOperation(value = "删除菜单",httpMethod = "DELETE")
    @RequestMapping(value = "{menuId}",method = RequestMethod.DELETE)
    public Result delete(@PathVariable("menuId") Integer menuId){
        int i = menuService.delete(menuId);
        if (i>0){
           return Result.ok(ResultCode.SUCCESS,i);
        }else{
           return Result.fail(ResultCode.FAIL,i);
        }
    }

}
